﻿using System.Linq;
using System.Web.Mvc;

using DAL;
using BLL;
using System.Text;
namespace App.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            #region Debug 正式发布要注释掉哦

            #endregion
            using (SysMenuBLL bll = new SysMenuBLL())
            {
                //SysPerson personInfo = new SysPerson();

                SysPerson personInfo = ((SysPerson)Session["person"]);
                if (personInfo == null) {
                    return RedirectToAction("LogOn", "Account");
                }
                string personId = personInfo.Id;
                ViewData["PerSon"] = personInfo.MyName;
                ViewData["LoginTime"] = System.DateTime.Now.ToString();
                StringBuilder strmenu = new StringBuilder();
                //var menuNedd = bll.GetAll().OrderBy(o => o.Sort).AsQueryable();
                SysEntities army = new SysEntities();
                var menuNedd = (from f in army.SysMenu
                                from m in f.SysRole
                                from p in army.SysPerson
                                from s in p.SysRole
                                //where f.State == "开启"
                                where m.Id == s.Id
                                where p.Id == personId
                                orderby f.Sort
                                select f).Distinct(); 

                if (menuNedd != null)
                {
                    //拼接菜单的字符串
                    var menuid = from f in menuNedd
                                 where f.ParentId == null
                                 orderby f.Sort
                                 select f;

                    var menus = menuNedd.Except(menuid).OrderBy(o => o.Sort);

                    int flag = 0;
                    foreach (var item in menuid)
                    {
                        flag++;
                        var name = (string.IsNullOrEmpty(item.Name) ? "" : item.Name);
                        var rootpath = "";// (string.IsNullOrEmpty(item.RootPath) ? "" : item.RootPath);
                        string str = "{@menuid@:@" + item.Id + "@,@icon@:@" + rootpath + "@,@menuname@:@" + name + "@,@menus@:[";

                        strmenu.Append(str);

                        var getmenu = GetMenus(item.Id, menus);
                        strmenu.Append(getmenu);

                        if (flag == menuid.Count())
                        {
                            strmenu.Append(@"]}");
                        }
                        else
                        {
                            strmenu.Append(@"]},");
                        }
                    }
                }

                string menuStr = strmenu.ToString().Replace('@', '"');
                if (!string.IsNullOrEmpty(menuStr) && menuStr.Length > 9)
                {

                    ViewData["Menu"] = menuStr;
                }
                else
                {
                    ViewData["Menu"] = string.Empty; ;
                }
            }
            return View();
        }
        public string GetMenus(string menuId, IQueryable<SysMenu> sysMenus)
        {
            var menus = from f in sysMenus
                        where f.ParentId == menuId
                        orderby f.Sort
                        select f;
            StringBuilder strmenu = new StringBuilder();
            int flag = 0;
            foreach (var item in menus)
            {
                flag++;

                var name = (string.IsNullOrEmpty(item.Name) ? "" : item.Name);
                var rootpath = "";// (string.IsNullOrEmpty(item.RootPath) ? "" : item.RootPath);
                var url = string.IsNullOrEmpty(item.Url) ? "" : item.Url;
                string str = "{@menuid@:@" + item.Id + "@,@icon@:@" + rootpath + "@,@menuname@:@" + name + "@,@url@:@" + url + "@}";

                strmenu.Append(str);
                if (flag != menus.Count())
                {
                    strmenu.Append(@",");
                }
            }
            return strmenu.ToString();
        }

    }
}
